package com.yuchen.ishop.web.app.seller;

import com.yuchen.ishop.entity.Shop;
import com.yuchen.ishop.service.seller.SellerRestClient;
import com.yuchen.ishop.service.seller.ShopRestClient;
import com.yuchen.ishop.vo.CookieUserInfo;
import com.yuchen.ishop.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 我的店铺
 */
@Controller
@RequestMapping(value = "/my/seller")
public class ShopAction extends BaseAction {

    @Autowired
    private ShopRestClient shopRestClient;

    @Autowired
    private SellerRestClient sellerRestClient;

    @RequestMapping(value = "myShop")
    public String myShop(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        modelMap.addAttribute("myMenu", "myShop");
        CookieUserInfo userInfo = cookie2UserInfo(request);
//        String role = (String) cookieMap.get("role");
        modelMap.addAttribute("seller", sellerRestClient.getById(userInfo.getId()));
        modelMap.addAttribute("shop", shopRestClient.findShopBySellerId(userInfo.getId()));
        return "my/seller/myShop";
    }

    @RequestMapping(value = "/saveOrUpdateShop", method = RequestMethod.POST)
    public String saveOrUpdateShop(Shop shop, Long pageNo, RedirectAttributes redirectAttributes) throws Exception {
        pageNo = pageNo == null ? 1l : pageNo;
        shop.setState(1l);
        if (shop.getId() == null) {
            shop.setCreateTime(getTime());
            shopRestClient.insertShop(shop);
        } else {
            shopRestClient.updateShop(shop);
        }
        return "redirect:/my/seller/myShop.htm";
    }
}
